const orderModel = require('../model/orderModel.js');

const orderController = {
    // 生成订单
    AddOrder: async (req,res) => {
        try {
            const result = await orderModel.AddOrder(req.body);
            console.log(result,'添加订单结果');
           if(result === 1){
                res.send({
                    code:1,
                    msg:"生成订单成功！"
                });
            }else if(result === 2){
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'生成订单出错啦！');
        }
    },
    // 修改订单
    UpdateOrder: async (req,res)=>{
        try {
            const result = await orderModel.UpdateOrder(req.body);
            if(result){
                res.send({
                    code:1,
                    msg:"更新订单成功",
                });
            }else{
                res.send({
                    code:0,
                    msg:"更新订单失败",
                });
            }
        } catch (error) {
            console.log(error,'更新订单出现error');
        }
    },
    //删除订单
    DeleteOrder: async (req,res)=>{
        try {
            const result = await orderModel.DeleteOrder(req.body);
            if(result){
                res.send({
                    code: 1,
                    msg: "删除订单成功",
                });
            }else{
                res.send({
                    code: 0,
                    msg: "删除订单失败",
                });
            }
        } catch (error) {
            console.log(error,'删除订单出现error');
        }
    },
    // 获取个人订单
    PersonalOrder: async (req,res) => {
        try {
            const {list,total} = await  orderModel.PersonalOrder(req.body);
            if(list.length > 0){
                res.send({
                    code:1,
                    msg:'获取用户订单列表成功',
                    list,
                    total,

                })
            } else{
                res.send({
                    code:0,
                    msg:'获取用户订单列表出错了,请填入正确的参数',
                })
            }
        }catch (error) {
            console.log(error,'获取用户订单列表controller层出错了');
        }
    },
    // 获取订单列表
    GetOrderList: async (req,res) => {
        console.log(req.body,'controller层获取订单列表调试');
        try {
            const {list,total} = await orderModel.GetOrderList(req.body);
            console.log(list,total,'订单列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取订单列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取订单列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取订单列表出现error");
        }
    },
    GetOrderDetail:async (req,res) => {
        try{
            const result = await orderModel.GetOrderDetail(req.params);
            console.log(result,'car-controller结果');
            if(result.length >0){
                res.status(200).send({
                    code:1,
                    detail:result[0],
                    msg:"获取订单详情成功"
                })
            }else{
                res.status(404).send({
                    code:0,
                    msg:"获取订单详情失败",
                });
            }
        }catch (error){
            console.log(error,'获取订单详情出现error');
        }
    }
};

module.exports = orderController;

